<?php
namespace App\Http\Controllers\Api\Goods;

use App\Http\Controllers\Controller;

use App\Models\Customer\Customer;  //会员表

use App\Models\Goods\GoodsComment; //商品评论表

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Redis as Redis;

class CommentsController extends Controller {


    /**
     * 商品评论
     * @param Request $request
     */
    public function search ( Request $request ) {

        $mainPostID = $request->input('id');  //商品主网购号
        $session_id = $request->input('session_id');
        $platform = $request->input('platform', 6);
        $grade = $request->input('grade', 0);
        $page = $request->input('page');
        $limit = $request->input('limit');

        if ( !ebsig_is_int($mainPostID ) ) {
            return response()->json(['code' => 10000, 'message' => '缺少参数：id']);
        }

        if ( empty( $session_id ) ) {
            $session_id = session_id();
            if ( empty( $session_id ) ) {
                return response()->json(['code' => 10001, 'message' => '缺少参数：session_id']);
            }
        }

        //评论等级
        if (!ebsig_is_int($grade)) {
            $grade = 0;
        }
        if (!ebsig_is_int($limit)) {
            $limit = 10;
        }
        if (!ebsig_is_int($page)) {
            $page = 1;
        }

        //获取客户号
        $pcustID = get_customer_id($session_id, $platform);

        /**
         * ==============================================
         * 获取评论列表
         * ==============================================
         */
        $wdh_comment_list_redis_index = 'WDH_GOODS_COMMENTS_LIST_'. $mainPostID.'_' . $grade;

        $comment_count = Redis::zCard($wdh_comment_list_redis_index); //检查评论数量

        if ( $comment_count == 0 ) {
            $where = [
                ['mainPostID',$mainPostID],
                ['commentStatus',1],
                ['display',1]
            ];

            if ( $grade > 0 ) {
                if ( $grade == 1 ) {  //好评
                    $where[] = ['commentLevel' ,'>=',4];
                    $where[] = ['commentLevel' ,'<=',5];
                } else if ( $grade == 2 ) { //中评
                    $where[] = ['commentLevel' ,'>=',2];
                    $where[] = ['commentLevel' ,'<=',3];
                } else if ( $grade == 3 ) { //差评
                    $where[] = ['commentLevel' ,1];
                }
            }

            $orWhere = [];
            if ( $pcustID > 0 ) {
                $orWhere =  ['display' => 0, 'pcustID' => $pcustID];
            }

            $field = [
                'pcustID','createTime','commentLevel','info','answerInfo'
            ];
            $comment_list = GoodsComment::select( $field )->where($where)->orWhere($orWhere)->orderBy('createTime', 'desc')->get()->toArray();
            if ( $comment_list ) {
                $comment_count = 0;
                foreach ($comment_list as &$item) {
                    Redis::zAdd($wdh_comment_list_redis_index, $comment_count, json_encode( $item ));
                    $comment_count++;
                }
                Redis::expire($wdh_comment_list_redis_index, 7200); //设置过期时间为2小时
            }
        }

        if ( $comment_count == 0 ) {
            return response()->json([ 'code' => 10002, 'message' => '没有评论数据' ]);
        }

        //从小到大分页获取团购id
        $comment_list = Redis::zRange( $wdh_comment_list_redis_index, ($page-1) * $limit, $page * $limit - 1);
        if (!$comment_list) {
            return response()->json([ 'code' => 10002, 'message' => '没有评论数据' ]);
        }

        //商品评论数组
        $comment = [];

        foreach ( $comment_list as &$item) {

            $json =json_decode($item,true);

            //检查会员信息
            $customer = Customer::select('custID')->find( $json['pcustID'] );

            if ( isMobile( $customer['custID'] ) ) {
                $cust_name = hideMobile( $customer['custID'] );
            } else {
                $cust_name =  $customer['custID'];
            }
            $comment[] = [
                'cust_name' => app_to_string( $cust_name ),
                'comment_time' => $json['createTime'],
                'comment_level' => app_to_int($json['commentLevel']),
                'comment_info' => app_to_string( $json['info']),
                'answerInfo' => app_to_string( $json['answerInfo'])
            ];
        }

        //查询各个状态的评论数量
        $comment_num = GoodsComment::getCommentCount( $mainPostID,$pcustID );

        return response()->json([
            'code' => 200 ,
            'message' => 'ok',
            'data' => [
                'page' => app_to_int($page),
                'limit' => app_to_int($limit),
                'count' => $comment_count,
                'total' => $comment_num['total_count'],
                'good_count' => $comment_num['good_count'],
                'mid_count' => $comment_num['mid_count'],
                'bad_count' => $comment_num['bad_count'],
                'list' => $comment
            ]
        ]);
    }

}